package com.muc.handler;

import com.muc.entity.CarDO;
import com.muc.service.CarService;
import com.muc.service.impl.CarServiceImpl;
import com.muc.view.AddCarView;
import com.muc.view.MainView;
import com.muc.view.UpdateCarView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddCarViewHandler implements ActionListener {
    private AddCarView addCarView;
    private MainView mainView;

    public AddCarViewHandler(AddCarView addCarView, MainView mainView) {
        this.addCarView = addCarView;
        this.mainView = mainView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if("添加".equals(text)) {
            CarService carService = new CarServiceImpl();
            CarDO carDO = addCarView.buildCarDO();
            boolean addResult = carService.add(carDO);
            if(addResult){
                //添加成功后的操作  重新加载表格
                mainView.reloadTable();
                addCarView.dispose();
            }else {
                //给出提示信息
                JOptionPane.showMessageDialog(addCarView,"添加失败!");
            }
        }
    }


}
